package com.mg.yygh.cmn.controller;

import com.mg.yygh.cmn.service.DictService;
import com.mg.yygh.common.result.Result;
import com.mg.yygh.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author M_Dragon
 * @date 2022/8/26/026 14:23
 */
@RestController
//@CrossOrigin
@Api(tags = "数据字典接口")
@RequestMapping("/admin/cmn/dict")
public class DictController {

    @Autowired
    private DictService dictService;

    @ApiOperation(value = "根据id查询子节点")
    @GetMapping("findChildData/{id}")
    public Result findChildData(@PathVariable("id") Long id) {
        List<Dict> dictList = dictService.findChildData(id);
        return Result.ok(dictList);
    }

    @ApiOperation(value = "导出数据字典")
    @GetMapping("exportData")
    public void exportData(HttpServletResponse response) {
        dictService.exportData(response);
    }

    @ApiOperation(value = "导入数据字典")
    @PostMapping("importData")
    public Result importData(MultipartFile file) {
        dictService.importData(file);
        return Result.ok();
    }

    @ApiOperation(value = "获取字典名称")
    @GetMapping("/getName/{value}/{dictCode}")
    public String getName(@PathVariable("value") String value,
                          @PathVariable("dictCode") String dictCode) {
        String name = dictService.getName(value, dictCode);

        return name;
    }

    @ApiOperation(value = "获取字典名称")
    @GetMapping("/getName/{value}")
    public String getName(@PathVariable("value") String value) {
        String name = dictService.getName(value, "");
        return name;
    }

    @ApiOperation(value = "根据dictCode获取下级节点")
    @GetMapping("/findByDictCode/{dictCode}")
    public Result findByDictCode(@PathVariable("dictCode") String dictCode) {
        List<Dict> dictList = dictService.findByDictCode(dictCode);
        return Result.ok(dictList);
    }

}
